package edu.zut.cs.software.perfect.attendance.service;

import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.DataFormatter;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

import edu.zut.cs.software.perfect.attendance.domain.AttendanceClerk;
import edu.zut.cs.software.perfect.base.service.GenericGenerator;

public class AttendanceEntityGeneratorTwo extends GenericGenerator {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = LogManager.getLogger(AttendanceEntityGeneratorTwo.class.getName());

	List<AttendanceClerk> attendanceClerkList;

	AttendanceClerkManager attendanceClerkManager;

	@Autowired
	public void setAttendanceClerkManager(AttendanceClerkManager attendanceClerkManager) {
		this.attendanceClerkManager = attendanceClerkManager;
	}

	@Before
	public void setUp() throws Exception {
		this.attendanceClerkList = new ArrayList<AttendanceClerk>();
		InputStream input = CourseAttendanceManagerTest.class.getResourceAsStream("/attendanceClerk.xlsx");
		@SuppressWarnings("resource")
		XSSFWorkbook wb = new XSSFWorkbook(input);
		XSSFSheet sheet = wb.getSheetAt(0);
		DataFormatter dataFormatter = new DataFormatter();
		for (int i = 1; i <= sheet.getLastRowNum(); i++) {
			Row row = sheet.getRow(i);
			AttendanceClerk s = new AttendanceClerk();
			for (int j = 0; j < row.getLastCellNum(); j++) {
				Cell cell = row.getCell(j);
				if (cell != null) {
					String value = dataFormatter.formatCellValue(cell);
					if (j == 1)
						s.setCourseCode(value);
					if (j == 2)
						s.setCourseName(value);
					if (j == 3)
						s.setTeacherName(value);
				}
			}
			this.attendanceClerkList.add(s);
		}
	}

	@Test
	public void test() {
		this.attendanceClerkManager.deleteAll();
		this.attendanceClerkManager.save(this.attendanceClerkList);
		Iterable<AttendanceClerk> result = this.attendanceClerkManager.findAll();

		if (logger.isInfoEnabled()) {
			logger.info("test() - Iterable<AttendanceClerk> result={}", result); //$NON-NLS-1$
		}
	}

}
